package com.colin.aiyi.ui.Home.yulu;

import android.content.Intent;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.colin.aiyi.R;
import com.colin.aiyi.app.Constant;
import com.colin.aiyi.app.MyApp;
import com.colin.aiyi.dao.ImpxUtils.ImpDiscourseList;
import com.colin.aiyi.dao.ImpxUtils.ImpDoLike;
import com.colin.aiyi.entity.BaseRequest;
import com.colin.aiyi.entity.response.BaseResponse;
import com.colin.aiyi.entity.DataEntity;
import com.colin.aiyi.entity.response.LoginResponse;
import com.colin.aiyi.entity.User;
import com.colin.aiyi.entity.YiJuAndYiShuoEntity;
import com.colin.aiyi.entity.response.YiJuAndYiShuoResponse;
import com.colin.aiyi.presenter.AbsP;
import com.colin.aiyi.ui.login.LoginAc;
import com.colin.aiyi.utils.SharePreferenceUtil;
import com.colin.aiyi.view.ShareBottomDialog;

import java.util.List;

import me.shaohui.shareutil.WXUtil;

/**
 * Created by hl on 2017/5/26.
 */

public class ImpPYuLu extends AbsP {
    IViewYuLu iViewYuLu;
    private int totalCount;
    private boolean pullMore=false;
    private String  contentType=Constant.TAB_TYPE_LIST;
    private String lastType=Constant.TAB_TYPE_LIST;  //上次类型
    BaseRequest request;
    private User user= SharePreferenceUtil.getObject(MyApp.getInstance().getApplicationContext(),User.class);
    private YiJuAndYiShuoEntity childYiShuoEntity;
    private BaseQuickAdapter childAdapter;
    private int childPosition;
    private ShareBottomDialog dialog;


    public ImpPYuLu(IViewYuLu iViewYuLu) {
        this.iViewYuLu = iViewYuLu;
    }

    @Override
    public void onSuccess(Object t) {
        if ( t instanceof  YiJuAndYiShuoResponse  ){
            if (((YiJuAndYiShuoResponse) t).getCode()==200)
            {
                dataPreprocess((YiJuAndYiShuoResponse) t);
            }else {
                sendMsgToView(((BaseResponse)t));
            } 
        }
        if (t instanceof LoginResponse) {// like结果集
            if (((LoginResponse) t).getCode() == 200) {
                if (childYiShuoEntity.getLikeCountActive()==0){
                    childYiShuoEntity.setLikeCount(childYiShuoEntity.getLikeCount()+1);
                    childYiShuoEntity.setLikeCountActive(1);
                    childAdapter.notifyItemChanged(childPosition);
                }else {
                    childYiShuoEntity.setLikeCount(childYiShuoEntity.getLikeCount()-1);
                    childYiShuoEntity.setLikeCountActive(0);
                    childAdapter.notifyItemChanged(childPosition);
                }
            }
            else if (((LoginResponse) t).getMessage().equals("你已经点过赞")){
                childYiShuoEntity.setLikeCountActive(1);
                childAdapter.notifyItemChanged(childPosition);
            }
            else {
                sendMsgToView(((BaseResponse) t));
            }
        }

    }

    private void dataPreprocess(YiJuAndYiShuoResponse d){
        DataEntity dataEntity = d.getData();
        totalCount=dataEntity.getCount();
        List<YiJuAndYiShuoEntity> list = dataEntity.getItems();
        if (!lastType.equals(contentType))
        {
            pullMore=false;
        }
        lastType=contentType;
        if (pullMore)
        {
            iViewYuLu.pullMoreComlite();
        }else {
            iViewYuLu.getData().clear();
            iViewYuLu.refreshComplite();
        }
        //// TODO: 2017/6/11 无数据加载
        if (list!=null&&list.size()>0)
        {
            iViewYuLu.getData().addAll(list);
        }else {
            iViewYuLu.refreshNoData();
        }
    }

    @Override
    public <V> void discourseList(V v) {
        super.discourseList(v);
        request= (BaseRequest) v;
        this.contentType=request.getOrderType();
        iService=new ImpDiscourseList();
        iService.getObject(this,request);
    }

    /********
     * 获取一说列表
     * @param //pageNum  目标页面
     * @param //cType  类型
     */

//    @Override
//    public void getAllT(String... type) {
//        this.contentType=type[type.length-1];
//        iService.getAllT(this,pageNum+"",contentType);
//    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        childYiShuoEntity = (YiJuAndYiShuoEntity) adapter.getData().get(position);
        childAdapter=adapter;
        childPosition=position;
        switch (view.getId())
        {
            case R.id.text_share:
//                ((TextView)view).getCompoundDrawables()[0].setLevel(1);
                if (user!=null&&user.getAccessToken()!=null&&user.getAccessToken().length()>1){

                }else {
                    Intent intent=new Intent(iViewYuLu.getContext(),LoginAc.class);
                    iViewYuLu.startNextAc(intent);
                    return;
                }

                if (dialog!=null&&dialog.isVisible()){
                    return;
                }
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                String url=Constant.discourse_list_H5+childYiShuoEntity.getId();
                dialog = new ShareBottomDialog();
                dialog.setUrl(url);
                dialog.setTitle(childYiShuoEntity.getContent());
                //   dialog.setBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.app_logo));
                dialog.setBitmap(WXUtil.getBitmap(Constant.BASE_URL+childYiShuoEntity.getPictureUrl()));
                dialog.show(iViewYuLu.getFM());
                    }
                }.start();
                break;
            case R.id.text_like:
                if (user!=null&&user.getAccessToken()!=null&&user.getAccessToken().length()>1){

                }else {
                    Intent intent=new Intent(iViewYuLu.getContext(),LoginAc.class);
                    iViewYuLu.startNextAc(intent);
                    return;
                }
                boolean operType=childYiShuoEntity.getLikeCountActive()==0;
                user.setOperType(operType);
                user.setOrderType("2");
                user.setSayId(childYiShuoEntity.getId()+"");
                like_doLike(user);
//                ImpOther.getInstance().doLike(operType,iViewYuLu.getData().get(position).getId()+"","2");
                break;
        }
    }


    @Override
    public <V> void like_doLike(V v) {
        super.like_doLike(v);
        iService=new ImpDoLike();
        iService.getObject(this,user);
    }

    public void onRefresh() {
        pullMore=false;
        request.setPageNum(1);
        discourseList(request);
       // getAllT(pageNum+"",contentType);
    }

    @Override
    public void onLoadMoreRequested() {
        if (iViewYuLu.getData().size() < totalCount) {
            pullMore=true;
            request.setPageNum(request.getPageNum()+1);
            discourseList(request);
          //  getAllT(pageNum+"",contentType);
        } else {
            iViewYuLu.pullMoreNoMore();
        }
    }
}
